package com.gbq.boot.web.service.impl;

import cn.hutool.crypto.SecureUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.gbq.boot.web.bean.Manager;
import com.gbq.boot.web.mapper.ManagerMapper;
import com.gbq.boot.web.service.ManagerService;
import com.gqb.boot.utils.BusinessException;
import com.gqb.boot.utils.enums.DataStatusEnum;
import org.apache.commons.lang.StringUtils;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.List;
import java.util.Random;

/**
 * @author aqian666
 */
@Service
@Transactional(rollbackFor=BusinessException.class)
public class ManagerServiceImpl  extends ServiceImpl<ManagerMapper, Manager> implements ManagerService {
    @Resource
    private ManagerMapper managerMapper;
    @Resource
    private HttpServletRequest request;
    @Resource
    private JavaMailSender javaMailSender;

    @Override
    public Manager findByName(String username)throws BusinessException  {
        //通过用户名查询
        QueryWrapper<Manager> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(Manager::getUsername,username);
        return  managerMapper.selectOne(queryWrapper);
    }

    @Override
    public void saveManager(Manager manager) throws BusinessException {
        try {
            //先验证邮箱是否已经被注册
            Manager byName = this.findByName(manager.getUsername());
            if (byName != null){
                throw new BusinessException("邮箱已经注册，不能再次注册。");
            }

            String codes = (String) request.getSession().getAttribute("code"+manager.getUsername());
            if (codes == null){
                throw new BusinessException("验证码已经失效。");
            }
            int code = Integer.parseInt(codes);
            if(manager.getCode() != code){
                throw new BusinessException("验证码输入错误。");
            }

            manager.setPassword(SecureUtil.md5(manager.getPassword()));
            managerMapper.insert(manager);
        } catch (BusinessException e) {
            throw new BusinessException("注册失败！",e.getErrorMessage());
        }
    }

    @Override
    public void sendOutEmail(String username) throws BusinessException{
        try {
            //先验证邮箱是否已经被注册
            Manager byName = this.findByName(username);
            if (byName != null){
                throw new BusinessException("邮箱已经注册，不能再次注册。");
            }

            MimeMessage message = javaMailSender.createMimeMessage();
            MimeMessageHelper helper = new MimeMessageHelper(message, true);
            helper.setFrom("289373410@qq.com");
            helper.setTo(username);
            helper.setSubject("标题：请收好你在阿前的博客的注册码");

            String checkCode = String.valueOf(new Random().nextInt(899999) + 100000);
            HttpSession session = request.getSession();
            session.setAttribute("code", checkCode);

            String str ="<h4>欢迎您在阿前的博客注册</h4>"+"<p style='color:#F00'>您的验证码为:"+checkCode+"</p>";
            //append("<p style='text-align:right'>右对齐</p>");
            helper.setText(str, true);

       /* FileSystemResource fileSystemResource=new FileSystemResource(new File("C:\\Users\\28937\\Desktop\\搞笑图片\\welcome.gif"));
        helper.addAttachment("啦啦啦",fileSystemResource);*/
            javaMailSender.send(message);
        } catch (BusinessException e) {
            throw new BusinessException(e.getErrorMessage());
        } catch (MessagingException e) {
            e.printStackTrace();
        }
    }

    @Override
    public List<Manager> getManagerByPage(Manager manager) {
        QueryWrapper<Manager> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().like(StringUtils.isNotBlank(manager.getUsername()),Manager::getUsername,manager.getUsername())
                             .like(StringUtils.isNotBlank(manager.getName()),Manager::getName,manager.getName())
                             .orderByDesc(Manager::getCreateTime)
                             .orderByDesc(Manager::getStatus);
        return managerMapper.selectList(queryWrapper);
    }

    @Override
    public void delManager(List<Integer> ids) throws BusinessException{
        try {
            managerMapper.deleteBatchIds(ids);
        }catch (BusinessException e){
            throw new BusinessException(e.getErrorMessage());
        }
    }

    @Override
    public void stopManager(List<Integer> ids, int key)throws BusinessException{
        try {
            List<Manager> managers = managerMapper.selectBatchIds(ids);
            if(managers.stream().anyMatch(x->x.getStatus()==key)){
                throw new BusinessException("已经是"+ DataStatusEnum.getValue(key)+"状态");
            }
            managers.forEach(x->x.setStatus(key));
            this.updateBatchById(managers);
        }catch (BusinessException e){
            throw new BusinessException(e.getErrorMessage());
        }
    }

}
